import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Description:lamda表达式，形式是：(参数列表)->{方法体}
 * @Author:AMark
 * @Date: 10/23/2023
 */
public class lamda_expression {
    private JFrame frame;
    private JButton okB,cancelB;
    lamda_expression(String title) {
        frame = new JFrame(title);
        frame.setSize(260, 150);
        frame.setLayout(new FlowLayout());
        okB = new JButton("确定");
        cancelB = new JButton("取消");
        frame.add(okB);
        frame.add(cancelB);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //创建匿名类进行确定按键的事件处理,直接在创建的时候同步写入函数的定义
        okB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setTitle("单击了确认按钮");
                System.out.print("匿名类");
            }
        });
        //使用lamda表达式进行取消的按钮事件处理
        cancelB.addActionListener(e -> frame.setTitle("单击了取消按钮"));
    }
    public static void main(String[] args) {
        lamda_expression le = new lamda_expression("简单演示");
    }
}
